package cn.wb.order.service.impl;

import cn.wb.base.service.impl.BaseServiceImpl;
import cn.wb.base.util.AjaxResult;
import cn.wb.order.domain.OrderPetAcquisition;
import cn.wb.order.dto.OrderConfirmDto;
import cn.wb.order.mapper.OrderPetAcquisitionMapper;
import cn.wb.order.service.IOrderPetAcquisitionService;
import cn.wb.pet.domain.Pet;
import cn.wb.pet.mapper.PetMapper;
import cn.wb.search.domain.SearchMasterMsg;
import cn.wb.search.mapper.SearchMasterMsgMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itsource
 * @since 2023-01-12
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {

    @Autowired
    private OrderPetAcquisitionMapper orderMapper;
    @Autowired
    private SearchMasterMsgMapper msgMapper;
    @Autowired
    private PetMapper petMapper;
    /**
     * 取消订单
     * @param id order的id
     * @return
     */
    @Override
    public AjaxResult cancel(Long id) {
        // 0.根据id查询order
        OrderPetAcquisition order = orderMapper.findById(id);
        // 1.收购单状态为取消
        order.setState(2);
        orderMapper.update(order);
        //2.修改寻主消息状态 -- 审核通过 1 并且shopId置空
        SearchMasterMsg msg = new SearchMasterMsg();
        msg.setId(order.getMsgId());
        msg.setState(1);
        msg.setShopId(null);
        msgMapper.updateById(msg);
        //3.删除宠物
        petMapper.delete(order.getPetId());
        return AjaxResult.me().success();
    }

    /**
     * 确认订单
     * @param dto
     * @return
     */
    @Override
    public AjaxResult confirm(OrderConfirmDto dto) {
        // 0.查询 order
        OrderPetAcquisition byId = orderMapper.findById(dto.getId());
        // 1.更改订单状态为 完成
        OrderPetAcquisition order = new OrderPetAcquisition();
        order.setId(dto.getId());
        order.setState(1);
        order.setPayType(dto.getPayType());
        order.setPrice(dto.getMoney());
        orderMapper.update(order);
        // 2.更改msg的状态为 完成
        SearchMasterMsg msg = new SearchMasterMsg();
        msg.setId(byId.getMsgId());
        msg.setState(4);
        msgMapper.update(msg);
        // 3.更改宠物信息 的 成本价
        Pet pet = new Pet();
        pet.setId(byId.getPetId());
        pet.setCostprice(dto.getMoney());
        petMapper.update(pet);
        return AjaxResult.me().success();
    }
}
